<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="评价类型" prop="orderId">
        <el-select
          v-model="queryParams"
          placeholder="请选择评价类型"
          clearable
          filterable
        >
          <el-option v-for="item in 3" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="用户名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodName">
        <el-input
          v-model="queryParams.goodName"
          placeholder="请输入商品分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价时间" prop="date">
        <el-date-picker
          v-model="queryParams.date"
          type="date"
          placeholder="请选择日期"
          clearable
          :editable="false"
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:item:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:item:export']">导出</el-button> -->
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="itemList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" align="center" /> -->
      <el-table-column label="ID" align="center" prop="id" width="50" />
      <el-table-column label="用户" align="center" prop="nickName" />
      <!-- <el-table-column label="平台" align="center" prop="goodTypeId" /> -->
      <el-table-column label="商品信息" prop="goodTypeName" width="200">
        <template slot-scope="{ row }">
          <div style="display: flex; align-items: center">
            <div>
              <image-preview
                :src="baseUrl + row.goodImg"
                :width="50"
                :height="50"
                style="margin-right: 10px"
              ></image-preview>
            </div>
            <div>{{ row.goodName }}</div>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="评价" align="center" prop="pics" width="300" /> -->
      <el-table-column label="详情" prop="storeGoodId" width="400">
        <template slot-scope="{ row }">
          <div>{{ row.comment }}</div>
          <div v-if="row.pics">
            <img
              :src="baseUrl + row.pics"
              alt=""
              style="width: 30px; height: 30px"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="评价回复"
        prop="merchantReplyContent"
        width="400"
      />
      <!-- <el-table-column label="置顶" align="center" prop="goodPrice" /> -->
      <el-table-column label="状态" align="center" prop="isReply">
        <template slot-scope="{ row }">
          {{ row.isReply == 0 ? "未回复" : "已回复" }}
        </template>
      </el-table-column>
      <el-table-column label="评价时间" prop="createTime" width="200" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="answerHandler(scope.row)"
            v-hasPermi="['system:item:edit']"
            >回复</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:item:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="16" :offset="0">
            <el-form-item label="用户名" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="16" :offset="0">
            <el-form-item label="评价时间" prop="createTime">
              <el-date-picker
                v-model="form.createTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd hh:ss:mm"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24" :offset="0">
            <el-form-item label="用户头像" prop="avatar">
              <image-upload
                :limit="1"
                @input="userImgHandler"
                :value="form.avatar"
              ></image-upload>
            </el-form-item>
          </el-col> -->
          <el-col :span="16" :offset="0">
            <el-form-item label="商品" prop="goodId">
              <el-autocomplete
                class="inline-input"
                v-model="selectGoods"
                :fetch-suggestions="querySearch"
                placeholder="请输入内容"
                @select="handleSelect"
                value-key="goodName"
                style="width: 100%"
              ></el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0">
            <el-form-item label="评价图片" prop="pics">
              <image-upload
                :limit="1"
                @input="evaluateImgHandler"
                :value="form.pics"
              ></image-upload>
            </el-form-item>
          </el-col>
          <el-col :span="16" :offset="0">
            <el-form-item label="评价" prop="comment">
              <el-input
                v-model="form.comment"
                type="textarea"
                placeholder="请输入商品名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 回复 -->
    <el-dialog title="评价回复" :visible.sync="answerOpen" width="600px">
      <el-input
        v-model="nswerForm.merchantReplyContent"
        type="textarea"
        placeholder="请输入回复内容，字数限制100字以内"
        size="normal"
        :rows="10"
        clearable
        maxlength="100"
        show-word-limit
      ></el-input>
      <span slot="footer">
        <el-button @click="answerClose">取消</el-button>
        <el-button type="primary" @click="answerEmit">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  commentList,
  commentDetail,
  commentAdd,
  commentEdit,
  commentDelete,
} from "@/api/system/order/evaluate";
import { listGood } from "@/api/system/goods/list";

export default {
  name: "Item",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单项目表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodName: null,
        date: null,
        nickName: null,
      },
      // 表单参数
      form: {},
      nswerForm: {},
      // 表单校验
      rules: {
        nickName: {
          required: true,
          message: "用户名不能为空",
          trigger: "blur",
        },
        createTime: {
          required: true,
          message: "评价时间不能为空",
          trigger: "blur",
        },
        goodId: { required: true, message: "商品不能为空", trigger: "blur" },
        comment: { required: true, message: "评价不能为空", trigger: "blur" },
      },
      baseUrl: process.env.VUE_APP_BASE_API,
      selectGoods: null,
      evaluateImgValue: null,
      userImgValue: null,
      answerOpen: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单项目列表 */
    getList() {
      this.loading = true;
      commentList(this.queryParams).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        goodId: null,
        pics: null,
        comment: null,
      };
      this.selectGoods = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单评价";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      commentDetail({ id }).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单评价";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          commentAdd(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(`是否确认删除该条评价？`)
        .then(function () {
          return commentDelete(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/comment/export",
        {
          ...this.queryParams,
        },
        `item_${new Date().getTime()}.xlsx`
      );
    },
    querySearch(queryString, cb) {
      let params = {
        pageSize: 10000,
        pageNum: 1,
        goodName: queryString,
      };

      listGood(params).then((res) => {
        cb(res.rows);
      });
    },
    // 商品选择
    handleSelect(value) {
      this.form.goodId = value.id;
    },
    // 评价商品上传
    evaluateImgHandler(value) {
      this.form.pics = value;
    },
    // 用户头像
    userImgHandler(value) {
      this.form.avatar = value;
    },
    // 回复
    answerHandler(row) {
      this.nswerForm = {
        id: row.id,
        merchantReplyContent: null,
      };
      this.answerOpen = true;
    },
    // 提交回复
    answerEmit() {
      if (!this.nswerForm.merchantReplyContent) {
        this.answerOpen = false;
        return;
      }
      this.nswerForm.isReply = 1;
      commentEdit(this.nswerForm).then((response) => {
        this.$modal.msgSuccess("回复成功");
        this.answerOpen = false;
        this.getList();
      });
    },
    // 取消回复
    answerClose() {
      this.answerOpen = false;
    },
  },
};
</script>
